package com.itheima.case2.serviceimp;

import com.itheima.case2.dao.UserMapper;
import com.itheima.case2.dao.UserRoleMapper;
import com.itheima.case2.pojo.User;
import com.itheima.case2.serviceiterface.UserService;
import com.itheima.case2.utils.SqlSessionUtil;
import com.itheima.case2.vo.PageBean;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserServiceImpl implements UserService {


    @Override
    public User login(String username, String password) {
        SqlSession sqlSession = SqlSessionUtil.getSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        User user = mapper.login(username,password);
        sqlSession.close();
        return user;
    }

    //实现分页查询功能的业务层---主体思想---封装
    //将web层传递过来的数据和其它处理得到的数据封装到PageBean对象中
    @Override
    public PageBean<User> findAllUsersByPage(int curPage, int pageSize) {
        //创建PageBean实例对象
        PageBean<User> pb = new PageBean<>();
        //封装数据
        SqlSession session = null;
        try {
            pb.setCurPage(curPage);
            pb.setPageSize(pageSize);
            int startIndex = pb.getStartIndex();
            //PageBean对象中还差list:每页的数据，  count：数据表中总的条数
            //mybatis那一套
            //获取sqlSession对象
            session = SqlSessionUtil.getSession();
            //获取接口代理对象
            UserMapper mapper = session.getMapper(UserMapper.class);
            //通过接口代理对象调用接口中的方法
            //调用分页查询的方法
            List<User> list = mapper.findAllUsersByPage(startIndex,pageSize);
            //封装到PageBean对象中
            pb.setList(list);
            //调用计算数据总的条数的方法
            int count = mapper.getAllUserCount();
            //封装到对象中
            pb.setCount(count);
        } finally {
            if (session != null){
                session.close();
            }

        }
        return pb;

    }

    //修改用户的信息
    //修改用户信息----业务层--传进来的新修改
    @Override
    public void updateUserById(User user) {
        //1.工具类获取接口UserMapper的代理对象-----操作t_user表
        UserMapper mapper = SqlSessionUtil.getProxyMapper(UserMapper.class);
        //2.接口代理对象调用修改用户信息的方法
        mapper.updateUserById(user);
        //3.工具类获取接口UserRoleMapper的代理对象
        UserRoleMapper userRoleMapper = SqlSessionUtil.getProxyMapper(UserRoleMapper.class);
        //4.取接口UserRoleMapper的代理对象调用方法，删除需要修改的用户在t_user_role的所有数据
        userRoleMapper.deleUserRole(Integer.parseInt(user.getId()));
        //5.获该修改用户的成员变量roleIds
        List<String> roleIds = user.getRoleIds();
        //6.遍历该用户成员变量的roleIds，将用户id，角色id重新插入t_user_role表中
        for (String roleId : roleIds) {
            userRoleMapper.addUserIdAndRoleId(Integer.parseInt(user.getId()),Integer.parseInt(roleId));
        }

    }

    //添加用户的信息
    @Override
    public void addUser(User user) {
        //添加用户信息----处理业务
        //1.mybatis
        //获取mybatis接口代理对象
        UserMapper mapper = SqlSessionUtil.getProxyMapper(UserMapper.class);

        //2.通过接口代理对象调用添加用户信息的方法
        //(1)首先向t_user表中添加信息
        mapper.addUser(user);
        //添加一个根据用户名查找用户的id的方法
        int uid = mapper.finUserIdByUsername(user.getUsername());

        //获取UserRoleMapper接口代理对象
        UserRoleMapper userRoleMapper = SqlSessionUtil.getProxyMapper(UserRoleMapper.class);

        //(2)遍历rolesIds集合，向t_user_role表中添加对象
        List<String> roleIds = user.getRoleIds();
        for (String roleId : roleIds) {
            userRoleMapper.addUserIdAndRoleId(uid,Integer.parseInt(roleId));
        }
    }


}
